package cn.wolfcode.ssm.web.controller;

import cn.wolfcode.ssm.domain.Department;
import cn.wolfcode.ssm.domain.Employee;
import cn.wolfcode.ssm.qo.EmployeeQueryObject;
import cn.wolfcode.ssm.qo.JsonResult;
import cn.wolfcode.ssm.service.IDepartmentService;
import cn.wolfcode.ssm.service.IEmployeeService;
import cn.wolfcode.ssm.service.IRoleService;
import cn.wolfcode.ssm.util.RequireAnnotation;
import com.github.pagehelper.PageInfo;
import com.sun.source.tree.TryTree;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("list")
    public String listAll(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        List<Department> departments = departmentService.ListAll();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("departments",departments);

        return "employee/list";
    }

    @RequireAnnotation(name = "员工删除",expression = "employee:delete")
    @RequestMapping("delete")
    public String delete(Long id){
        employeeService.deleteById(id);
        return "redirect:/employee/list";
    }

    @RequireAnnotation(name = "员工编辑/保存",expression = "employee:saveOrUpdate")
    @RequestMapping("input")
    public String input(Model model,Long id){

        if(id != null){
            Employee employee = employeeService.selectById(id);
            model.addAttribute("employee",employee);
        }
        model.addAttribute("departments",departmentService.ListAll());
        model.addAttribute("roles",roleService.listAll());
        return "employee/input";
    }

    @RequireAnnotation(name = "员工编辑/保存",expression = "employee:saveOrUpdate")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Model model, Employee employee, Long[] ids){
            if(employee.getId() != null){
                employeeService.updateById(employee,ids);
            }else {
                employeeService.save(employee,ids);
            }
            return new JsonResult();
    }

    //跳转到nopermission页面
    @RequestMapping("nopermission")
    public String nopermission(){
        return "common/nopermission";
    }


    @RequestMapping("checkUsername")
    @ResponseBody
    public Map<String,Boolean> input(String username){

       Employee employee = employeeService.checkUsername(username);
        HashMap<String, Boolean> map = new HashMap<>();
        map.put("valid",employee == null);
        return map;

    }

    @RequireAnnotation(name = "员工信息导出",expression = "employee:exportXls")
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response,EmployeeQueryObject qo) throws IOException {
        //文件下载的响应头（让浏览器访问资源的的时候以下载的方式打开）
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        //创建Excel文件
        Workbook wb = employeeService.exportXls(qo);
        //
        wb.write(response.getOutputStream());
    }

    @RequireAnnotation(name = "员工信息导入",expression = "employee:importXls")
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file,String password) throws IOException {
        //获取操作xml的文件对象
        //创建Excel文件
        String msg = employeeService.importXls(file, password);
        if (msg != null){
            return new JsonResult(false,msg);
        }
        return new JsonResult();
    }

    @RequireAnnotation(name = "员工禁用/恢复",expression = "employee:status")
    @RequestMapping("/status")
    public String status(Long id) {
        employeeService.updateForStatus(id);
        return "redirect:/employee/list";

    }




}
